<?php
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$link = mysqli_connect('localhost', 'root', '112233456', 'test');
if (!$link) {
    exit('数据库连接失败');
}
mysqli_query($link, 'set names utf8');
$res = mysqli_query($link, 'select id from student');
$row_cnt = mysqli_num_rows($res);
$pageSize = 5;
$pageMax = ceil($row_cnt / $pageSize);
$offset = ($page - 1) * $pageSize;
$sql = "select no,name,id,address from student limit {$offset},{$pageSize}";
$res = mysqli_query($link, $sql);
$rows = array();
while ($row = mysqli_fetch_assoc($res)) {
    $rows[] = $row;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>分页</title>
</head>
<body>
<table border="1">
    <tr>
        <th>学号</th>
        <th>姓名</th>
        <th>身份证号</th>
        <th>住址</th>
    </tr>
    <?php foreach ($rows as $k => $v) { ?>
        <tr>
            <td><?php echo $v['no'] ?></td>
            <td><?php echo $v['name'] ?></td>
            <td><?php echo $v['id'] ?></td>
            <td><?php echo $v['address'] ?></td>
        </tr>
    <?php } ?>
    <tr>
        <td colspan="4">
            <a href="./two.php?page=1">首页</a>
            <a href="./two.php?page=<?php echo $page <= 1 ? $page : $page - 1 ?>">上一页</a>
            <a href="./two.php?page=<?php echo $page >= $pageMax ? $page : $page + 1 ?>">下一页</a>
            <a href="./two.php?page=<?php echo $pageMax ?>">末页</a>
        </td>
    </tr>
</table>
</body>
</html>
